﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HR_SearchEmployee : System.Web.UI.Page
{
    CommonMethods cMethod = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDepartments();
            BindSubDepartment();
        }
    }
    [System.Web.Services.WebMethod]
    public static List<Employee_GetEmployees_Result> SearchEmployee(Int32 Dept_Id, Int32 SubDept_Id)
    {
        using (POSEntities db = new POSEntities())
        {
            try
            {
                var Employees = db.Employee_GetEmployees(Dept_Id, SubDept_Id, 1).ToList();
                return Employees;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
    }

    private void BindDepartments()
    {
        int branchId = 3;
            DDL_Department.DataSource = cMethod.getDepartmentWithBranchId(branchId);
            DDL_Department.DataBind();
    }
    private void BindSubDepartment()
    {
        if (DDL_Department.Items.Count > 0)
        {
            DDL_SubDepartment.DataSource = cMethod.getSubDepartmentWithDepartmentId(Convert.ToInt32(DDL_Department.SelectedValue));
            DDL_SubDepartment.DataBind();
          
        }
    }


    protected void lbtnViewLogin_Click(object sender, EventArgs e)
    {

    }

    private void getEmployees()
    {
        using (POSEntities db=new POSEntities())
        {
            Int32 subdeptId = Convert.ToInt32(DDL_SubDepartment.SelectedValue);
            var employees = (from e in db.Employees
                             join d in db.Departments on e.Dept_ID equals d.Dept_ID
                             join sd in db.SubDepartments on e.SubDeptID equals sd.SubDeptID
                             join c in db.Cities on e.City_ID equals c.City_ID
                             where e.SubDeptID == subdeptId && e.IsDeleted == false
                             select new { e.Branch.BranchName, d.Dept_Name, sd.SubDeptName, e.EmpId, e.EmpName, e.Cell, e.Address, c.City_Name })
                          .ToList();
            GVEmployees.DataSource = employees;
            GVEmployees.DataBind();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (DDL_SubDepartment.Items.Count>0)
        {
            getEmployees();
        }

    }

    protected void lbtnDeleteEmployee_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = (LinkButton)sender;
        Int32 id = Convert.ToInt32(lbtn.CommandArgument);
        using (POSEntities db=new POSEntities())
        {
            var login = (from u in db.Users where u.EmpId==id select u).FirstOrDefault();
            login.Is_Active = false;
            db.SaveChanges();

            var empl = (from emp in db.Employees where emp.EmpId == id select emp).FirstOrDefault();
            empl.IsDeleted = true;
            db.SaveChanges();
        }
    }
}